package org.gw.building.ctrl;

import org.apache.commons.lang3.StringUtils;
import org.gw.building.model.Tyxxb;
import org.gw.building.model.Tyxxcjb;
import org.gw.building.model.Xxcjshzt;
import org.gw.building.model.base.Tyxxcjzb;
import org.gw.building.repositories.TyxxbRepository;
import org.gw.building.repositories.TyxxcjbRepository;
import org.gw.building.repositories.TyxxcjzbRepository;
import org.gw.ylc.base.context.RequestContextHolder;
import org.gw.ylc.base.interceptor.RequestPermission;
import org.gw.ylc.base.model.UserInfo;
import org.gw.ylc.base.util.PermissionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author qianwx
 */
@RestController
@RequestMapping("tyxxAudit")
public class TyxxcjAuditCtrl {

  @Autowired
  private TyxxcjzbRepository tyxxcjzbRepository;

  @Autowired
  private TyxxcjbRepository tyxxcjbRepository;

  @Autowired
  private TyxxbRepository tyxxbRepository;

  @RequestMapping("list")
  @RequestPermission({RequestPermission.DEPARTMENT, RequestPermission.SCHOOL})
  public Page<Tyxxcjzb> list(String department, String shzt) {
    UserInfo userInfo = RequestContextHolder.get().getUser();
    int type = userInfo.getType();
    if (type == 1) {
      department = userInfo.getDepartment();
    }
    Page<Tyxxcjzb> cjzbs = tyxxcjzbRepository.queryCjzb(department, shzt, RequestContextHolder.get().getPageable());
    return cjzbs;
  }

  @RequestPermission({RequestPermission.DEPARTMENT, RequestPermission.SCHOOL})
  @RequestMapping("listTyxx")
  public Page<Tyxxcjb> listTyxx(String xh, String cjzb) {
    return tyxxcjbRepository.findTyxx(PermissionUtil.toLikeString(xh), cjzb, RequestContextHolder.get().getPageable());
  }

  @RequestMapping("audit")
  @RequestPermission({RequestPermission.DEPARTMENT, RequestPermission.SCHOOL})
  @Transactional
  public Object audit(String cjzbid, boolean pass) {
    Assert.hasText(cjzbid, "无效的审核信息");
    UserInfo userInfo = RequestContextHolder.get().getUser();
    int type = userInfo.getType();
    String dm = pass ? (type == 1 ? "30" : "99") : (type == 1 ? "10" : "20");
    Tyxxcjzb cjzb = tyxxcjzbRepository.findOne(cjzbid);
    Assert.notNull(cjzb, "无效的审核信息");
    if (type == 1) {
      cjzb.setYxshr(userInfo.getName());
      cjzb.setYxshr(userInfo.getUid());
      cjzb.setYxshrq(new Date());
    } else if (type == 2) {
      cjzb.setShr(userInfo.getName());
      cjzb.setShrid(userInfo.getUid());
      cjzb.setShrq(new Date());
    }
    cjzb.setShzt(new Xxcjshzt(dm));
    if(pass && type == 2){
      copyTyxx(cjzbid);
    }
    return tyxxcjzbRepository.save(cjzb);
  }

  @Transactional
  void copyTyxx(String cjzbid) {
    List<Tyxxcjb> cjbs = tyxxcjbRepository.findByCjzbid(cjzbid);
    final List<Tyxxb> tyxxbs = new ArrayList<>();
    cjbs.forEach(cjb ->{
      String dm = cjb.getGxzt().getDm();
      if(StringUtils.equals(dm,"4")){
        return;
      }
      Tyxxb tyxxb = new Tyxxb();
      BeanUtils.copyProperties(cjb,tyxxb);
      if(StringUtils.equals(dm,"3")){
        tyxxb.setScbz(true);
      }
      tyxxbs.add(tyxxb);
    });
    tyxxbRepository.save(tyxxbs);
  }
}
